#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: gtk_vfo.py
Date: April 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

VFO handling

"""

#-----------------------------------------------------------------------
# System imports
import sys
from time import sleep
import subprocess

# Import GTK libraries
import pygtk
import gtk
import gtk.glade
import gobject

# Application imports
import utils

#======================================================================
# The VFO class
class VfoGui:
  
    #---------------------------------------------------------------------
    def __init__(self, main_tree, ev_prx, name_prx, digits, signed, caps, persist_service):
        self.main_tree = main_tree
        self.digits = digits
        self.signed = signed
        self.caps = caps
        self.persist_service = persist_service
        self.ignore = False
        self.smhz = '000'
        self.skhz = '000'
        self.shz = '000'
        self.mhz_sz = '22'
        self.mhz_colour = 'red'
        self.khz_sz = '22'
        self.khz_colour = 'red'
        self.hz_sz = '22'
        self.hz_colour = 'red'
        self.sign_sz = '22'
        self.pub = None
        
        #-----------------------------------------------------------------------
        # Wire up the events
        if digits >= 1:
            dic = { 
                ev_prx + "1hz_enter_notify_event" : self.enter_1hz,
                ev_prx + "1hz_leave_notify_event" : self.leave_1hz,
                ev_prx + "1hz_scroll_event" : self.scroll_1hz,
            }
            self.l_1hz = self.main_tree.get_widget(name_prx + '1hz')
            self.main_tree.signal_autoconnect (dic)
        if digits >= 2:
            dic = { 
                ev_prx + "10hz_enter_notify_event" : self.enter_10hz,
                ev_prx + "10hz_leave_notify_event" : self.leave_10hz,
                ev_prx + "10hz_scroll_event" : self.scroll_10hz,
            }
            self.l_10hz = self.main_tree.get_widget(name_prx + '10hz')
            self.main_tree.signal_autoconnect (dic)
        if digits >= 3:
            dic = { 
                ev_prx + "100hz_enter_notify_event" : self.enter_100hz,
                ev_prx + "100hz_leave_notify_event" : self.leave_100hz,
                ev_prx + "100hz_scroll_event" : self.scroll_100hz,
            }
            self.l_100hz = self.main_tree.get_widget(name_prx + '100hz')
            self.main_tree.signal_autoconnect (dic)
        if digits >= 4:
            dic = { 
                ev_prx + "1khz_enter_notify_event" : self.enter_1khz,
            ev_prx + "1khz_leave_notify_event" : self.leave_1khz,
            ev_prx + "1khz_scroll_event" : self.scroll_1khz,
            }
            self.l_1khz = self.main_tree.get_widget(name_prx + '1khz')
            self.main_tree.signal_autoconnect (dic)
        if digits >= 5:
            dic = { 
                ev_prx + "10khz_enter_notify_event" : self.enter_10khz,
                ev_prx + "10khz_leave_notify_event" : self.leave_10khz,
                ev_prx + "10khz_scroll_event" : self.scroll_10khz,
            }
            self.l_10khz = self.main_tree.get_widget(name_prx + '10khz')
            self.main_tree.signal_autoconnect (dic)
        if digits >= 6:
            dic = { 
                ev_prx + "100khz_enter_notify_event" : self.enter_100khz,
                ev_prx + "100khz_leave_notify_event" : self.leave_100khz,
                ev_prx + "100khz_scroll_event" : self.scroll_100khz,
            }
            self.l_100khz = self.main_tree.get_widget(name_prx + '100khz')
            self.main_tree.signal_autoconnect (dic)
        if digits >= 7:
            dic = { 
                ev_prx + "1mhz_enter_notify_event" : self.enter_1mhz,
                ev_prx + "1mhz_leave_notify_event" : self.leave_1mhz,
                ev_prx + "1mhz_scroll_event" : self.scroll_1mhz,
            }
            self.l_1mhz = self.main_tree.get_widget(name_prx + '1mhz') 
            self.main_tree.signal_autoconnect (dic)
        if digits >= 8:
            dic = { 
                ev_prx + "10mhz_enter_notify_event" : self.enter_10mhz,
                ev_prx + "10mhz_leave_notify_event" : self.leave_10mhz,
                ev_prx + "10mhz_scroll_event" : self.scroll_10mhz,
            }
            self.l_10mhz = self.main_tree.get_widget(name_prx + '10mhz')
            self.main_tree.signal_autoconnect (dic)
        if digits >= 9:
            dic = { 
                ev_prx + "100mhz_enter_notify_event" : self.enter_100mhz,
                ev_prx + "100mhz_leave_notify_event" : self.leave_100mhz,
                ev_prx + "100mhz_scroll_event" : self.scroll_100mhz,
            }
            self.l_100mhz = self.main_tree.get_widget(name_prx + '100mhz')
            self.main_tree.signal_autoconnect (dic)
        
        if signed:
            self.l_o_sign = self.main_tree.get_widget(name_prx + 'sign')
            
        # Avoid circular events
        self.ignore = False
        
        # Adjust for capabilities
    
    #--------------------------------------------------------------------- 
    def set_publisher(self, pub):
        self.pub = pub
        
    #--------------------------------------------------------------------- 
    def set_colour(self, mhz, khz, hz):
        self.mhz_colour = mhz
        self.khz_colour = khz
        self.hz_colour = hz
    
    #--------------------------------------------------------------------- 
    def set_mode(self, mode):
        # The vfo or oscillator we currently are
        # Not used at present
        self.mode = mode
        
    #=====================================================================
    # Events for each of the 9 possible frequency digits
    # Mouse enter, mouse leave and wheel scroll
    #---------------------------------------------------------------------
    def enter_100mhz(self, widget, x):
        l = self.l_100mhz.get_label()
        l = '<span font_desc=\"Times '+self.mhz_sz+'\" foreground=\"'+self.mhz_colour+'\" underline = \"single\">'+self.smhz[0]+'</span>'
        self.l_100mhz.set_label(l)
    
    #---------------------------------------------------------------------
    def leave_100mhz(self, widget, x):
        l = self.l_100mhz.get_label()
        l = '<span font_desc=\"Times '+self.mhz_sz+'\" foreground=\"'+self.mhz_colour+'\">'+self.smhz[0]+'</span>'
        l = self.l_100mhz.set_label(l)
      
    #---------------------------------------------------------------------
    def scroll_100mhz(self, widget, e):
        if e.direction == gtk.gdk.SCROLL_UP:
            self.freq_inc(100.0)
        else:
            self.freq_inc(-100.0)
      
    #---------------------------------------------------------------------
    def enter_10mhz(self, widget, x):
        l = self.l_10mhz.get_label()
        l = '<span font_desc=\"Times '+self.mhz_sz+'\" foreground=\"'+self.mhz_colour+'\" underline = \"single\">'+self.smhz[1]+'</span>'
        self.l_10mhz.set_label(l)
    
    #---------------------------------------------------------------------
    def leave_10mhz(self, widget, x):
        l = self.l_10mhz.get_label()
        l = '<span font_desc=\"Times '+self.mhz_sz+'\" foreground=\"'+self.mhz_colour+'\">'+self.smhz[1]+'</span>'
        l = self.l_10mhz.set_label(l)
      
    #---------------------------------------------------------------------
    def scroll_10mhz(self, widget, e):
        if e.direction == gtk.gdk.SCROLL_UP:
            self.freq_inc(10.0)
        else:
            self.freq_inc(-10.0)
      
    #---------------------------------------------------------------------
    def enter_1mhz(self, widget, x):
        l = self.l_1mhz.get_label()
        l = '<span font_desc=\"Times '+self.mhz_sz+'\" foreground=\"'+self.mhz_colour+'\" underline = \"single\">'+self.smhz[2]+'</span>'
        self.l_1mhz.set_label(l)
    
    #---------------------------------------------------------------------
    def leave_1mhz(self, widget, x):
        l = self.l_1mhz.get_label()
        l = '<span font_desc=\"Times '+self.mhz_sz+'\" foreground=\"'+self.mhz_colour+'\">'+self.smhz[2]+'</span>'
        l = self.l_1mhz.set_label(l)
      
    #---------------------------------------------------------------------
    def scroll_1mhz(self, widget, e):
        if e.direction == gtk.gdk.SCROLL_UP:
            self.freq_inc(1.0)
        else:
            self.freq_inc(-1.0)
      
    #---------------------------------------------------------------------
    def enter_100khz(self, widget, x):
        l = self.l_100khz.get_label()
        l = '<span font_desc=\"Times '+self.khz_sz+'\" foreground=\"'+self.khz_colour+'\" underline = \"single\">'+self.skhz[0]+'</span>'
        self.l_100khz.set_label(l)
    
    #---------------------------------------------------------------------
    def leave_100khz(self, widget, x):
        l = self.l_100khz.get_label()
        l = '<span font_desc=\"Times '+self.khz_sz+'\" foreground=\"'+self.khz_colour+'\">'+self.skhz[0]+'</span>'
        l = self.l_100khz.set_label(l)
      
    #---------------------------------------------------------------------
    def scroll_100khz(self, widget, e):
        if e.direction == gtk.gdk.SCROLL_UP:
            self.freq_inc(0.1)
        else:
            self.freq_inc(-0.1)
      
    #---------------------------------------------------------------------
    def enter_10khz(self, widget, x):
        l = self.l_10khz.get_label()
        l = '<span font_desc=\"Times '+self.khz_sz+'\" foreground=\"'+self.khz_colour+'\" underline = \"single\">'+self.skhz[1]+'</span>'
        self.l_10khz.set_label(l)
    
    #---------------------------------------------------------------------
    def leave_10khz(self, widget, x):
        l = self.l_10khz.get_label()
        l = '<span font_desc=\"Times '+self.khz_sz+'\" foreground=\"'+self.khz_colour+'\">'+self.skhz[1]+'</span>'
        l = self.l_10khz.set_label(l)
      
    #---------------------------------------------------------------------
    def scroll_10khz(self, widget, e):
        if e.direction == gtk.gdk.SCROLL_UP:
            self.freq_inc(0.01)
        else:
            self.freq_inc(-0.01)
      
    #---------------------------------------------------------------------
    def enter_1khz(self, widget, x):
        l = self.l_1khz.get_label()
        l = '<span font_desc=\"Times '+self.khz_sz+'\" foreground=\"'+self.khz_colour+'\" underline = \"single\">'+self.skhz[2]+'</span>'
        self.l_1khz.set_label(l)
    
    #---------------------------------------------------------------------
    def leave_1khz(self, widget, x):
        l = self.l_1khz.get_label()
        l = '<span font_desc=\"Times '+self.khz_sz+'\" foreground=\"'+self.khz_colour+'\">'+self.skhz[2]+'</span>'
        l = self.l_1khz.set_label(l)
      
    #---------------------------------------------------------------------
    def scroll_1khz(self, widget, e):
        if e.direction == gtk.gdk.SCROLL_UP:
            self.freq_inc(0.001)
        else:
            self.freq_inc(-0.001)
            
    #---------------------------------------------------------------------
    def enter_100hz(self, widget, x):
        l = self.l_100hz.get_label()
        l = '<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.hz_colour+'\" underline = \"single\">'+self.shz[0]+'</span>'
        self.l_100hz.set_label(l)
    
    #---------------------------------------------------------------------
    def leave_100hz(self, widget, x):
        l = self.l_100hz.get_label()
        l = '<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.hz_colour+'\">'+self.shz[0]+'</span>'
        l = self.l_100hz.set_label(l)
      
    #---------------------------------------------------------------------
    def scroll_100hz(self, widget, e):
        if e.direction == gtk.gdk.SCROLL_UP:
            self.freq_inc(0.0001)
        else:
            self.freq_inc(-0.0001)
      
    #---------------------------------------------------------------------
    def enter_10hz(self, widget, x):
        l = self.l_10hz.get_label()
        #l = l.replace('16', '20')
        l = '<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.hz_colour+'\" underline = \"single\">'+self.shz[1]+'</span>'
        self.l_10hz.set_label(l)
    
    #---------------------------------------------------------------------
    def leave_10hz(self, widget, x):
        l = self.l_10hz.get_label()
        l = '<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.hz_colour+'\">'+self.shz[1]+'</span>'
        l = self.l_10hz.set_label(l)
      
    #---------------------------------------------------------------------
    def scroll_10hz(self, widget, e):
        if e.direction == gtk.gdk.SCROLL_UP:
            self.freq_inc(0.00001)        
        else:
            self.freq_inc(-0.00001)
      
    #---------------------------------------------------------------------
    def enter_1hz(self, widget, x):
        l = self.l_1hz.get_label()
        l = '<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.hz_colour+'\" underline = \"single\">'+self.shz[2]+'</span>'
        self.l_1hz.set_label(l)
    
    #---------------------------------------------------------------------
    def leave_1hz(self, widget, x):
        l = self.l_1hz.get_label()
        l = '<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.hz_colour+'\">'+self.shz[2]+'</span>'
        l = self.l_1hz.set_label(l)
      
    #---------------------------------------------------------------------
    def scroll_1hz(self, widget, e):
        if e.direction == gtk.gdk.SCROLL_UP:
            self.freq_inc(0.000001)
        else:
            self.freq_inc(-0.000001)
  
    #---------------------------------------------------------------------
    #---------------------------------------------------------------------
    # Send the freq inc event
    def freq_inc(self, data):
        if self.pub:
            self.pub(round(data, 6))
        
    #---------------------------------------------------------------------  
    # Update events
    #---------------------------------------------------------------------
    def freq_update(self, freq, offset=None):
        # Actual tuned frequency
        if offset:
            actual_freq = freq + offset
        else:
            actual_freq = freq
        # Convert frequency to string parts   
        smhz, skhz, shz = utils.FreqToString(actual_freq)
        self.smhz = smhz
        self.skhz = skhz
        self.shz = shz

        # Set the whole frequency
        if self.digits >= 1:
            self.l_1hz.set_label('<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.hz_colour+'\">'+shz[2]+'</span>')
        if self.digits >= 2:
            self.l_10hz.set_label('<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.hz_colour+'\">'+shz[1]+'</span>')
        if self.digits >= 3:
            self.l_100hz.set_label('<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.hz_colour+'\">'+shz[0]+'</span>')
        if self.digits >= 4:
            self.l_1khz.set_label('<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.khz_colour+'\">'+skhz[2]+'</span>')
        if self.digits >= 5:
            self.l_10khz.set_label('<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.khz_colour+'\">'+skhz[1]+'</span>')
        if self.digits >= 6:
            self.l_100khz.set_label('<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.khz_colour+'\">'+skhz[0]+'</span>')
        if self.digits >= 7:
            self.l_1mhz.set_label('<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.mhz_colour+'\">'+smhz[2]+'</span>') 
        if self.digits >= 8:
            self.l_10mhz.set_label('<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.mhz_colour+'\">'+smhz[1]+'</span>')
        if self.digits >= 9:
            self.l_100mhz.set_label('<span font_desc=\"Times '+self.hz_sz+'\" foreground=\"'+self.mhz_colour+'\">'+smhz[0]+'</span>')
        
        if self.signed:
            if actual_freq > 0.0: sign = '+'
            elif actual_freq < 0.0: sign = '-'
            else: sign = ' '
            self.l_o_sign.set_label('<span font_desc=\"Times '+self.sign_sz+'\" foreground=\"red\">'+sign+'</span>')